package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.AdminInfo;
import com.dao.AdminDao;
import com.utils.Des;
import com.utils.PageInfo;
import com.utils.PageUtil;
import com.utils.StrUtil;



@WebServlet("/AdminServlet.do")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminDao _adminDao=new AdminDao();
   
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		String flag=request.getParameter("flag");
		
		if("addAdmin".equals(flag)) {
			this.addAdmin(request,response);
		}else if("manage".equals(flag)){
			this.manage(request,response);
		}else if("lock".equals(flag)) {
			this.lock(request,response);
		}else if("del".equals(flag)){
			this.del(request,response);
		}else if("searchforupdate".equals(flag)){
			this.searchForUpdate(request, response);
		}else if("updateadmin".equals(flag)){
			this.updateAdmin(request,response);
		}else if("updatePassword".equals(flag)){
			this.updatePassword(request,response);
		}else if("delMore".equals(flag)){
			this.delMore(request,response);
		}
	}
	
	//更新用户密码
		private void updatePassword(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
				AdminInfo admin=(AdminInfo)request.getSession().getAttribute("session_admin");
				String oldPassword=request.getParameter("oldPassword");
				
				if(!Des.encStr(oldPassword).equals(admin.getPassword())){
					request.setAttribute("msg", "操作失败! 旧密码输入不正确!");
					request.getRequestDispatcher("/admin/password_edit.jsp").forward(request, response);
				}
				
				else{
					String newPassword=request.getParameter("password");		
					if(_adminDao.updatePassword(admin.getId(),Des.encStr(newPassword))==1){
						request.setAttribute("msg", "密码更新成功!");
						request.getRequestDispatcher("/admin/password_edit.jsp").forward(request, response);
					}
				}
			
		}
	//更新用户信息
		private void updateAdmin(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			int adminId=Integer.parseInt(request.getParameter("id"));
			AdminInfo adminInfo=_adminDao.getAdminById(adminId);
			
			adminInfo.setNote(request.getParameter("note"));
			if(_adminDao.updateAdmin(adminInfo)==1){
				request.setAttribute("msg","管理员信息更新成功! ");
				request.setAttribute("adminInfo",adminInfo);   //用adminInfo 不用 admin的原因是防止和 session 中的admin 混淆
				request.getRequestDispatcher("/admin/admin_edit.jsp").forward(request, response);
			}	
		}
	//查询出用户信息,转到修改页面
		private void searchForUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int adminId=Integer.parseInt(request.getParameter("id"));
			AdminInfo admin=_adminDao.getAdminById(adminId);
			request.setAttribute("adminInfo", admin);
			request.getRequestDispatcher("/admin/admin_edit.jsp").forward(request, response);	
		}
	//根据Id删除单个用户，但不会真的删除
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		_adminDao.del(id);
		request.setAttribute("msg", "删除成功");
		this.manage(request, response);
		
	}
	//删除多个管理员
	private void delMore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idSql=request.getParameter("idSql");
		idSql=idSql.substring(0,idSql.length()-1);
		String [] ids=idSql.split(",");
		for(int i=0;i<ids.length;i++) {
			Integer id=Integer.parseInt(ids[i]);
			_adminDao.del(id);
		}
		request.setAttribute("msg", "删除成功");
		this.manage(request, response);
	}
	
	//用于解锁及锁定用户
	private void lock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到当前用户状态
		String lockState=request.getParameter("lockState");
		//解锁用户
		if("0".equals(lockState)){
			int id=Integer.parseInt(request.getParameter("id"));
			_adminDao.upDateById(id, "1");
			this.manage(request, response);
		}else {//锁定用户
			int id=Integer.parseInt(request.getParameter("id"));
			_adminDao.upDateById(id, "0");
			this.manage(request, response);
		}

		
	}

	//用户维护主界面
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到当前页数
		int pageIndex=1;	
		String pageIndexStr=request.getParameter("pageIndex");
		if(!StrUtil.isNullOrEmpty(pageIndexStr)){
			pageIndex=Integer.parseInt(pageIndexStr);
		}
		//得到数据总条数
		int rowCount=_adminDao.getAdminCount();
		//得到PageInfo对象
		PageInfo pageInfo=PageUtil.getPageInfo(rowCount, 10, pageIndex);
		//得到所有的管理员
		List<AdminInfo> adminList=_adminDao.getAdminList(pageInfo);
		
		request.setAttribute("adminList", adminList);
		request.setAttribute("pageInfo", pageInfo);
		request.getRequestDispatcher("/admin/admin_manage.jsp").forward(request, response);
	}

	//添加管理员
	private void addAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String adminName=request.getParameter("adminName");
		String password=request.getParameter("password");
		String note=request.getParameter("note");
	 
		AdminInfo admin=new AdminInfo();
		admin.setAdminName(adminName);
		admin.setPassword(Des.encStr(password));
		admin.setNote(note);
		admin.setState("1");

		//验证用户名是否存在
		int result=_adminDao.addAdmin(admin);
		if(result==1){
			request.setAttribute("admin", admin);
			request.setAttribute("msg", "添加成功");
			request.getRequestDispatcher("/admin/admin_add.jsp").forward(request, response);
		}else{
			request.setAttribute("msg", "添加失败,该用户已存在");
			request.getRequestDispatcher("/admin/admin_add.jsp").forward(request, response);
		}
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
